package com.hyfrogx.modules.app.utils;

import java.util.Map;
import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import cn.hutool.core.util.ObjectUtil;
import com.hyfrogx.common.qcloud.GameUtils;
import sun.misc.BASE64Encoder;

/**
 * 	腾讯云签名工具类
 *  @author Li9527
 *  @since 2017-6-29 13:12:58
 *  @version 1.0.0
 *  {@link ://www.qcloud.com/document/product/266/9756}
 *
 */
@SuppressWarnings("restriction")
public class VodSignUtils {


    public static int m_iSignValidDuration = 3600 * 24 * 60;
    private static final String HMAC_ALGORITHM = "HmacSHA1";
    private static final String CONTENT_CHARSET = "UTF-8";

    public static String getSign(Map map) {
        String sign = GetUploadSignature(null, null, map.get("isTranscode"), map.get("isScreenshot"));
        return sign;
    }

    public static String getSign(Integer isTranscode,Integer isScreenshot) {
        if (isTranscode != 1) {
            isTranscode = 0;
        }
        if (isScreenshot != 1) {
            isScreenshot = 0;
        }
        String sign = GetUploadSignature(null, null, isTranscode, isScreenshot);
        return sign;
    }

    /**
     * 获取签名
     *
     * @param random
     *            随机整数
     * @param m_qwNowTime
     *            秒时间
     * @param isTranscode
     *            是否转码0不转,1转码,null不参与签名拼接
     * @param isScreenshot
     *            是否截图0不截,1截图,null不参与签名拼接
     * @return
     */
    public static String GetUploadSignature(Integer random, Long m_qwNowTime, Object isTranscode, Object isScreenshot) {
        if (random == null) {
            random = new Random().nextInt(java.lang.Integer.MAX_VALUE);
        }
        if (m_qwNowTime == null) {
            m_qwNowTime = System.currentTimeMillis() / 1000;
        }
        String strSign = "";
        String contextStr = "";
        long endTime = (m_qwNowTime + m_iSignValidDuration);
        try {
            // 构造签名明文串的参数，云API 密钥中的 Secret ID。
            contextStr += "secretId=" + java.net.URLEncoder.encode(GameUtils.SecretId, CONTENT_CHARSET);
            // 构造签名明文串的参数，当前 Unix 时间。
            contextStr += "&currentTimeStamp=" + m_qwNowTime;
            // 构造签名明文串的参数，签名到期 Unix 时间。
            // expireTime =currentTimeStamp +签名有效时长
            // 签名有效时长最大取值为7776000，即90天
            contextStr += "&expireTime=" + endTime;
            // 构造签名明文串的参数，无符号32位随机数。
            contextStr += "&random=" + random;
            // 是否转码
            if (isTranscode != null) {
                contextStr += "&isTranscode=" + isTranscode;
            }
            if (isScreenshot != null) {
                contextStr += "&isScreenshot=" + isScreenshot;
            }
            Mac mac = Mac.getInstance(HMAC_ALGORITHM);
            SecretKeySpec secretKey = new SecretKeySpec(GameUtils.StrSecKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
            mac.init(secretKey);
            byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));
            byte[] sigBuf = byteMerger(hash, contextStr.getBytes(CONTENT_CHARSET));
            strSign = new String(new BASE64Encoder().encode(sigBuf).getBytes());
            strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
            return strSign;
        } catch (Exception e) {
            System.out.print(e.toString());
            return null;
        }
    }

    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }


//	/**
//	 * 根据内容,生成腾讯云签名
//	 *
//	 * @param contextStr
//	 * @return
//	 */
//	public String getSign(String contextStr) {
//		String strSign = "";
//		try {
//			Mac mac = Mac.getInstance(HMAC_ALGORITHM);
//			SecretKeySpec secretKey = new SecretKeySpec(m_strSecKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
//			mac.init(secretKey);
//			byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));
//			byte[] sigBuf = byteMerger(hash, contextStr.getBytes(CONTENT_CHARSET));
//			strSign = new String(new BASE64Encoder().encode(sigBuf).getBytes());
//			strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
//		} catch (Exception e) {
//			return "";
//		}
//		return strSign;
//	}
}
